// 从 API 模块中导入所有需要使用的函数
import { 
    sendEmailApi, 
    uploadApi, 
    showEmailApi, 
    getFileListApi, 
    getDeletedApi, 
    deleteFileApi, 
    receivedByPOP3Api, 
    updateEmailStateApi, 
    downloadAttachmentApi, 
    getReceiveProtocolApi, 
    setReceiveProtocolApi, 
    receivedByIMAPApi 
  } from "@/api/email";
  
  // 定义所有的 actions
  const actions = {
    // 发送邮件操作
    sendEmail({}, data) {
      // 输出发送邮件操作的数据
      console.log("sendEmail action called with data:", data);
      // 返回一个 Promise 对象
      return new Promise((resolve, reject) => {
        // 调用发送邮件的 API 函数
        sendEmailApi(data).then(res => {
          // 如果返回状态为成功
          if (res.status === 10000) {
            // 解析 Promise，并返回成功的状态
            resolve(res);
          } else {
            // 如果返回状态为失败，则拒绝 Promise，并返回失败的状态
            reject(res);
          }
        }).catch(error => {
          // 捕获可能的错误，并返回失败的状态
          reject(error);
        });
      });
    },
    // 显示邮件操作
    showEmail({}, data) {
      return new Promise((resolve, reject) => {
        showEmailApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 上传操作
    upload({}, data) {
      console.log("upload:", data);
      return new Promise((resolve, reject) => {
        uploadApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 获取文件列表操作
    getFileList({}, data) {
      return new Promise((resolve, reject) => {
        getFileListApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 删除文件操作
    deleteFile({}, data) {
      return new Promise((resolve, reject) => {
        deleteFileApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 通过POP3接收邮件操作
    receivedByPOP3({}, data) {
      return new Promise((resolve, reject) => {
        receivedByPOP3Api(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 下载附件操作
    downloadAttachment({}, data) {
      return new Promise((resolve, reject) => {
        downloadAttachmentApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 设置接收协议操作
    setReceiveProtocol({}, data) {
      return new Promise((resolve, reject) => {
        setReceiveProtocolApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 获取接收协议操作
    getReceiveProtocol({}, data) {
      return new Promise((resolve, reject) => {
        getReceiveProtocolApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 通过IMAP接收邮件操作
    receivedByIMAP({}, data) {
      return new Promise((resolve, reject) => {
        receivedByIMAPApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 更新邮件状态操作
    updateEmailState({}, data) {
      return new Promise((resolve, reject) => {
        updateEmailStateApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    },
    // 获取已删除邮件操作
    getDeleted({}, data) {
      return new Promise((resolve, reject) => {
        getDeletedApi(data).then(res => {
          if (res.status === 10000) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(error => {
          reject(error);
        });
      });
    }
  };
  
  // 导出模块
  export default {
    namespaced: true,
    actions
  };
  